package com.dominator.utils.sms;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.dominator.utils.system.Constants;
import com.dominator.utils.network.HttpPoster;
import com.dominator.utils.system.PropertiesLoader;
import com.dominator.utils.system.VerifyCodeUtil;
import com.dominator.utils.exception.ApiException;
/**
 * 龙举短信发送
 * @author Administrator
 *
 */
public class ShiYuanSmsUtils {

	public static PropertiesLoader propertiesLoader = new PropertiesLoader("sysconfig.properties");
	 /**
     * 发送短信(示远)
     * @param phoneNum
     * @param verifyCode
     * @param type
     * @param dto
     * @throws ApiException
     */
	public static void sendSms(JSONObject dto) throws ApiException {
		System.out.println("shiyuan发送短信开始");
		String content = "";
		switch (dto.getInteger("stype")) {
		case 1:
			content = propertiesLoader.getProperty("sms.notice.register_tpl");
			content = String.format(content, dto.getInteger("verifyCode"));
			break;
		case 2:
			content = propertiesLoader.getProperty("sms.notice.login_tpl");
			content = String.format(content, dto.getInteger("verifyCode"));
			break;
		case 3:
			content = propertiesLoader.getProperty("sms.notice.find_login_tpl");
			content = String.format(content, dto.getInteger("verifyCode"));
			break;
		case 4:
			content = propertiesLoader.getProperty("sms.notice.recharge_tpl");
			try {
				content = String.format(content, dto.getString("phoneNum"),dto.getString("payMoney"),new String(dto.getString("lesson").getBytes("utf-8"), "ISO-8859-1"));
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				throw new ApiException(Constants.SYS_ERROR, "模版编码错误");
			}
			break;
		case 5:
			content = propertiesLoader.getProperty("sms.notice.change_bean_tpl");
			content = String.format(content, dto.getString("phoneNum"),dto.getString("payMoney"),dto.getString("studyBean"),dto.getString("profit"));
			break;
		case 6:
			content = propertiesLoader.getProperty("sms.notice.order_bean_tpl");
			content = String.format(content, dto.getString("phoneNum"),dto.getString("lesson"),dto.getString("usedStudyBean"),dto.getString("totalStudyBean"));
			break;
		}

		try {
			content =propertiesLoader.getProperty("sms.notice.sign")+content+propertiesLoader.getProperty("sms.notice.tail_tpl");
			System.out.println(content);
			content = new String(content.getBytes("ISO-8859-1"), "utf-8");
			System.out.println(content);
		} catch (UnsupportedEncodingException e) {
			throw new ApiException(Constants.SYS_ERROR, "模版编码错误");
		}
		content = String.format(content, VerifyCodeUtil.getCode6());
		content = "您的验证码是：168168。";
		System.out.println(content);
		String returnString=null;
		System.out.println("开始发送短信----示远");
		Map<String, String> params = new HashMap<String, String>();
		params.put("account",  propertiesLoader.getProperty("sms.shiyuan.user_code"));
		params.put("pswd", propertiesLoader.getProperty("sms.shiyuan.user_pass"));
		params.put("mobile",dto.getString("phoneNum"));
		params.put("msg", content);
		params.put("needstatus", propertiesLoader.getProperty("sms.shiyuan.needstatus"));
		params.put("product", propertiesLoader.getProperty("sms.shiyuan.product"));
		params.put("extno", propertiesLoader.getProperty("sms.shiyuan.extno"));
		try {
			returnString = HttpPoster.post(propertiesLoader.getProperty("sms.shiyuan.url"), params);
			System.out.println(returnString);
			//TODO 处理返回值,参见HTTP协议文档
		} catch (Exception e) {
			//TODO 处理异常
			e.printStackTrace();
		}
		System.out.println("longju发送短信结束-----示远");
		String[] arr = returnString.split(",")[1].split("\n");
		if(arr[0].equals("0")){
			System.out.println("发送成功");
		}else{
			String message = null;
			switch (returnString.split(",")[1]) {
			case "101":		
				message = "无此用户";
				break;

			case "102":		
				message = "密码错";
				break;

			case "103":		
				message = "提交过快（提交速度超过流速限制）";
				break;

			case "104":		
				message = "系统忙（因平台侧原因，暂时无法处理提交的短信）";
				break;

			case "105":		
				message = "敏感短信（短信内容包含敏感词）";
				break;

			case "106":		
				message = "消息长度错（>536或<=0）";
				break;

			case "107":		
				message = "包含错误的手机号码";
				break;

			case "108":		
				message = "手机号码个数错（群发>50000或<=0）";
				break;

			case "109":		
				message = "无发送额度（该用户可用短信数已使用完）";
				break;

			case "110":		
				message = "不在发送时间内(验证码通知7*24小时发送)";
				break;

			case "111":		
				message = "超出该账户当月发送额度限制";
				break;

			case "112":		
				message = "无此产品，用户没有订购该产品";
				break;

			case "113":		
				message = "extno格式错（非数字或者长度不对）";
				break;

			case "115":		
				message = "自动审核驳回";
				break;

			case "116":		
				message = "签名不合法，未带签名（用户必须带签名的前提下）";
				break;

			case "117":		
				message = "IP地址认证错,请求调用的IP地址不是系统登记的IP地址";
				break;

			case "118":		
				message = "用户没有相应的发送权限";
				break;

			case "119":		
				message = "用户已过期";
				break;
			case "120":
				message = "内容不在白名单中";	
				break;

			default:
				break;
			}
			System.out.println("发送失败！原因： " + returnString.split(",")[1]);
			throw new ApiException(Constants.SYS_ERROR, message);
		}
//		Map<String, String> params = new HashMap<String, String>();
//		params.put("action", "send");
//		params.put("userid", "63");
//		params.put("account", propertiesLoader.getProperty("sms.shiyuan.user_code"));
//		params.put("password", propertiesLoader.getProperty("sms.shiyuan.user_pass"));
//		params.put("sendTime", "");
//		params.put("extno", "");
//		params.put("mobile", dto.getString("phoneNum"));
//		params.put("content", content);
//		String res = null;
//		System.out.println("longju开始发送短信");
//		try {
//			res = HttpPoster.post(propertiesLoader.getProperty("sms.shiyuan.url"), params);
//		} catch (Exception e) {
//			e.printStackTrace();
//			throw new ApiException(Constants.SYS_ERROR, "发送请求失败");
//		}
//		System.out.println("longju发送短信结束");
//
//		System.out.println(res);
//		String jsonStr = XmlConverUtil.xmltoJson(res);
//		System.out.println(jsonStr);
//		JSONObject json = (JSONObject) JSON.parse(jsonStr);
//		if ("Success".equals(json.getString("returnstatus"))) {
//			if ("ok".equals(json.getString("message"))) {
//				System.out.println("发送成功");
//			} else {
//				System.out.println("发送失败！原因： " + json.getString("message"));
//				throw new ApiException(Constants.SYS_ERROR, json.getString("message"));
//			}
//		} else {
//			System.out.println("调用短信接口失败");
//			throw new ApiException(Constants.SYS_ERROR, "调用短信接口失败");
//		}

	}
	
	public static void main(String[] args) throws Exception {

		// String xml = propertiesLoader.getProperty("xml.test");
		// String jsonStr = XmlConverUtil.xmltoJson(xml);
		// System.out.println(jsonStr);
		// JSONObject json = (JSONObject) JSON.parse(jsonStr);
		// if ("Success".equals(json.getString("returnstatus"))) {
		//
		// if ("ok".equals(json.getString("message"))) {
		// System.out.println("发送成功");
		// } else {
		// System.out.println("发送失败！原因： " + json.getString("message"));
		// }
		//
		// } else {
		// System.out.println("调用短信接口失败");
		// }
		JSONObject dto =new JSONObject();
		dto.put("stype", 1);
		dto.put("verifyCode", 123456);
		dto.put("phoneNum", "18761876386");
		 ShiYuanSmsUtils.sendSms(dto);

	}

}
